package top.colordreams.tenant;

import top.colordreams.tenant.resolver.CurrentTenantResolver;

import java.util.Objects;

/**
 * 默认租户上下文
 *
 * @author 秋辞未寒
 */
public class DefaultTenantContext implements TenantContext {

    private final TenantConfig tenantConfig;
    private final CurrentTenantResolver currentTenantResolver;

    public DefaultTenantContext(TenantConfig tenantConfig, CurrentTenantResolver currentTenantResolver) {
        this.tenantConfig = tenantConfig;
        this.currentTenantResolver = currentTenantResolver;
    }

    @Override
    public String getDefaultTenantId() {
        if (tenantConfig.isDefaultTenantIsRoot() && Objects.isNull(tenantConfig.getDefaultTenantId())) {
            return tenantConfig.getRootTenantId();
        }
        return tenantConfig.getDefaultTenantId();
    }

    @Override
    public String getCurrentTenantId() {
        return currentTenantResolver.resolveCurrentTenantId();
    }

    @Override
    public boolean defaultTenantIsRoot() {
        return tenantConfig.isDefaultTenantIsRoot();
    }

    @Override
    public String getRootTenantId() {
        if (tenantConfig.isDefaultTenantIsRoot() && Objects.isNull(tenantConfig.getRootTenantId())) {
            return tenantConfig.getDefaultTenantId();
        }
        return tenantConfig.getRootTenantId();
    }
}
